﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WarehouseManager.Core;
using WarehouseManager.Model;
using WarehouseManager.DataAccess;

namespace WarehouseManager.View
{
    public partial class FormHanghoa : Form
    {
        HanghoaAccess hhaccess = new HanghoaAccess();
        DataTable dshh = new DataTable();
        public FormHanghoa()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void FormHanghoa_Load(object sender, EventArgs e)
        {
            dshh = hhaccess.Laytoanbohh();
            dataGridView1.DataSource = dshh;

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMahanghoa.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTenhanghoa.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtDongia.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtDonvitinh.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtSoluong.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            cbbNhacungcap.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Hanghoa hh = new Hanghoa(txtMahanghoa.Text, txtTenhanghoa.Text, float.Parse(txtDongia.Text), txtDonvitinh.Text, int.Parse(txtSoluong.Text), cbbNhacungcap.Text);
            if (hhaccess.themhh(hh) == 0)
            {
                MessageBox.Show("mã hh đã có");
            }
            else
            {
                MessageBox.Show("thêm thành công");
            }
            FormHanghoa_Load(sender, e);

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Hanghoa hh = new Hanghoa(txtMahanghoa.Text, txtTenhanghoa.Text, float.Parse(txtDongia.Text), txtDonvitinh.Text, int.Parse(txtSoluong.Text), cbbNhacungcap.Text);
            if (hhaccess.suahh(hh) == 0)
            {
                MessageBox.Show("mã hh ko có");
            }
            else
            {
                MessageBox.Show("sua thành công");
            }
            FormHanghoa_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Hanghoa hh = new Hanghoa(txtMahanghoa.Text, txtTenhanghoa.Text, float.Parse(txtDongia.Text), txtDonvitinh.Text, int.Parse(txtSoluong.Text), cbbNhacungcap.Text);
            if (hhaccess.xoahh(hh) == 0)
            {
                MessageBox.Show("mã hh đã có");
            }
            else
            {
                MessageBox.Show("xóa thành công");
            }
            FormHanghoa_Load(sender, e);
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            DataView dataView = new DataView();
            if (cbbTimkiem.SelectedItem==cbbTimkiem.Items[0])
            {
                dataView = hhaccess.timkiemMahh(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
            else
            {
                dataView = hhaccess.timkiemtenhh(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {

                txtMahanghoa.ReadOnly = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMahanghoa.ReadOnly = true;
                btnThem.Enabled = false;
            }
        }
    }
}
